package collection;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/** List集合 */
public class ListDemo {
    public static void main(String[] args) {
        List<String> list = new ArrayList<>();
        list.add("one");
        list.add("two");
        list.add("three");
        list.add("four");
        list.add("five");
        String a = list.get(2);
        System.out.println(a);

        for (String s : list) {
            System.out.println(s);
        }
        for (int i = 0; i < list.size(); i++) {
            System.out.println(list.get(i));//找下标
        }
        Iterator<String> it = list.iterator();
        while (it.hasNext()){
            System.out.println(it.next());
        }

        System.out.println("----------------------");
        //list.set(2)将指定下标的元素替换成给的元素并返回
        String o = list.set(2,"six");
        System.out.println("list:"+list);
        System.out.println(o);
        //list.remove(2)删除元素并返回
        String s = list.remove(2);
        System.out.println(s);
        System.out.println("list"+list);
        //void add(int index,E e)将给定元素添加的到指定位置
        list.add(3,"seven");//在下标为3的元素插入seven
        System.out.println(list);

    }
}
